﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using DocuServe.Data.Context;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Context;

namespace DocuServe.Data
{
    internal class SessionManager
    {
        private static Configuration Configuration { get; set; }
        private static ISessionContextManager SessionContext { get; set; }
        private static ISessionFactory SessionFactory { get; set; }

        #region SessionManager : Constructors.
        static SessionManager()
        {
            SessionManager.Configuration = new Configuration().Configure();
            SessionManager.SessionFactory = SessionManager.Configuration.BuildSessionFactory();
            SessionManager.SessionContext = new SessionContextManagerFactory(SessionManager.Configuration).Create();
        }
        #endregion

        #region SessionManager : Methods.
        internal static void Begin()
        {
            ISession session = SessionManager.Open();
            if (session.Transaction == null || !session.Transaction.IsActive)
                session.BeginTransaction();
        }

        internal static void Close()
        {
            ISession session = SessionManager.GetCurrentSession();
            if (session != null && session.IsOpen)
            {
                session.Close();
                SessionManager.SessionContext.Unbind(SessionManager.SessionFactory);
            }
        }

        internal static void Commit()
        {
            ISession session = SessionManager.SessionFactory.GetCurrentSession();
            if (session != null && session.IsOpen)
                session.Transaction.Commit();
        }

        internal static void Evict(object obj)
        {
            ISession session = SessionManager.GetCurrentSession();
            if (session != null)
                session.Evict(obj);
        }

        internal static void Flush()
        {
            ISession session = SessionManager.GetCurrentSession();
            if (session != null)
                session.Flush();
        }

        internal static ISession GetCurrentSession()
        {
            return SessionManager.HasOpenSession() ?
                SessionManager.SessionFactory.GetCurrentSession() : null;
        }

        internal static bool HasOpenSession()
        {
            return SessionManager.SessionContext.HasBind(SessionManager.SessionFactory);
        }

        internal static ISession Open()
        {
            ISession session;
            if (SessionManager.SessionContext.HasBind(SessionManager.SessionFactory))
            {
                session = SessionManager.SessionFactory.GetCurrentSession();
            }
            else
            {
                session = SessionManager.SessionFactory.OpenSession();
                SessionManager.SessionContext.Bind(session);
            }
            return session;
        }

        internal static void Rollback()
        {
            ISession session = SessionManager.GetCurrentSession();
            if (session != null && session.Transaction != null && session.Transaction.IsActive)
                session.Transaction.Rollback();
        }
        #endregion
    }
}
